﻿Imports LinhKienMayTinh.CommonMethods

Public Class ProductDetail
    Inherits BasePage

#Region "Khai báo các event"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xCategory As String = String.Empty
        Dim xManufactory As String = String.Empty
        Dim xProduct As String = String.Empty
        Dim nProductPerPage As Integer = 1
        Dim dsProduct As DataSet
        Dim dsSameTypeProductList As DataSet

        Try
            xCategory = Me.QueryString("category")
            xManufactory = Me.QueryString("manufactory")
            xProduct = Me.QueryString("product")

            dsProduct = GetProductDetail(xCategory, xManufactory, xProduct)
            If dsProduct IsNot Nothing AndAlso dsProduct.Tables(0).Rows.Count > 0 Then
                ucProductDetail.DataSource = dsProduct
                ucProductDetail.DataBind()

                dsSameTypeProductList = GetSameTypeProductList(xCategory, xManufactory, xProduct, Nz(dsProduct.Tables(0).Rows(0)("PRiCE"), 0))
                ucSameTypeProductList.DataSource = dsSameTypeProductList
                ucSameTypeProductList.DataBind()

                Me.Header.Title = Me.AppSettings("SiteName") & " | " & CStr(dsProduct.Tables(0).Rows(0)("PRODUCT_NAME"))
            Else
                Context.Items("ErrorCode") = CommonConstants.LINK_NAME_NOT_EXIST
                Server.Transfer("~/Pages/Error.aspx")
            End If
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

    Private Sub ProductDetail_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim master As LinhKienMayTinhMasterPage = DirectCast(Me.Page.Master, LinhKienMayTinhMasterPage)
        'master.CssLinkAdd(ResolveUrl("~/Style/ProductDetail.css"))
        'master.CssLinkAdd(ResolveUrl("~/Style/ProductList.css"))
        master.CssLinkAdd(ResolveUrl("~/Style/ProductListMetroStyle.css"))
        master.CssLinkAdd(ResolveUrl("~/Style/ProductDetailMetroStyle.css"))
    End Sub

#End Region

#Region "Khai báo các function method"

    Protected Function GetProductDetail(pCategory As String, pManufactory As String, ByVal pProduct As String) As DataSet
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = New DBAccess(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.NAME          AS  PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.LINK_NAME,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.CATEGORY,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.MANUFACTORY,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.PRICE,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.CONDITION,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.WARRANTY,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "	   T_PRODUCT.DESCRIPTION,"
            xSql = xSql & vbCrLf & "	   T_CATEGORY.CODE,"
            xSql = xSql & vbCrLf & "	   T_CATEGORY.NAME         AS  CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "	   T_CATEGORY.LINK_NAME,"
            xSql = xSql & vbCrLf & "	   T_MANUFACTORY.CODE,"
            xSql = xSql & vbCrLf & "	   T_MANUFACTORY.NAME      AS  MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "	   T_MANUFACTORY.LINK_NAME"
            xSql = xSql & vbCrLf & "  FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "  LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "	ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND		T_CATEGORY.LINK_NAME     = @CATEGORY"
            xSql = xSql & vbCrLf & "AND	    T_MANUFACTORY.LINK_NAME  = @MANUFACTORY"
            xSql = xSql & vbCrLf & "AND  	T_PRODUCT.LINK_NAME      = @PRODUCT"

            xSql = Replace(xSql, "@CATEGORY", ToSqlStr(pCategory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@MANUFACTORY", ToSqlStr(pManufactory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@PRODUCT", ToSqlStr(pProduct, SqlDataType.DataType_String))

            ds = dataAccess.GetDBDataset(xSql)

            Return ds

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Protected Function GetSameTypeProductList(ByVal pCategory As String, ByVal pManufactory As String, ByVal pProduct As String, ByVal pPrice As Decimal) As DataSet
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = New DBAccess(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT TOP 12"
            xSql = xSql & vbCrLf & "        DAT.*"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "("
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.NAME         AS CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.NAME      AS MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND		T_CATEGORY.LINK_NAME	= @CATEGORY"
            xSql = xSql & vbCrLf & "AND		T_MANUFACTORY.LINK_NAME = @MANUFACTORY"
            xSql = xSql & vbCrLf & "AND		T_PRODUCT.LINK_NAME     != @PRODUCT"
            xSql = xSql & vbCrLf & "AND		T_PRODUCT.PRICE BETWEEN @PRICE - 1000000 AND @PRICE + 1000000"
            xSql = xSql & vbCrLf & "UNION ALL"
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.NAME         AS CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.NAME      AS MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND		T_CATEGORY.LINK_NAME	= @CATEGORY"
            xSql = xSql & vbCrLf & "AND		T_MANUFACTORY.LINK_NAME != @MANUFACTORY"
            xSql = xSql & vbCrLf & "AND		T_PRODUCT.PRICE BETWEEN @PRICE - 1000000 AND @PRICE + 1000000"
            xSql = xSql & vbCrLf & ") DAT"

            xSql = Replace(xSql, "@CATEGORY", ToSqlStr(pCategory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@MANUFACTORY", ToSqlStr(pManufactory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@PRODUCT", ToSqlStr(pProduct, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@PRICE", ToSqlStr(pPrice, SqlDataType.DataType_String))

            ds = dataAccess.GetDBDataset(xSql)

            Return ds

        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region

End Class